﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Unite;
using C.Entities;
using ChanExt;
using MySoft.Data;
using System.Text;
using LitJson;
using Newtonsoft.Json.Linq;

namespace ArtLand.OA.Project.HR
{
    public partial class HR_SalaryChange : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txt_Empid.Text = Request["empid"];
                BindDrp();
                BindGrid();
                YMBind();
            }
        }

        protected void BindDrp()
        {
            for (int i = 1; i < 13; i++)
            {
                Drp_Mouth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
            }
            Drp_Mouth.SelectedValue = DateTime.Now.Month.ToString();
            Drp_Year.Items.Add(new ListItem((DateTime.Now.Year - 1).ToString() + "年", (DateTime.Now.Year - 1).ToString()));
            Drp_Year.Items.Add(new ListItem((DateTime.Now.Year).ToString() + "年", (DateTime.Now.Year).ToString()));
            Drp_Year.SelectedValue = DateTime.Now.Year.ToString();
        }


        protected void YMBind()
        {
            HR_ESALY he = TbsDbSession.Default.From<HR_ESALY>().Where(HR_ESALY._.EMPLID == Request["empid"] && HR_ESALY._.YEAR == int.Parse(Drp_Year.SelectedValue) && HR_ESALY._.MONTH == int.Parse(Drp_Mouth.SelectedValue)).ToFirst();
            if (he != null)
            {
                txt_ABSENC.Text = he.ABSENC.Value.ToString();
                txt_ASALRY.Text = he.ASALRY.Value.ToString();
                txt_CSALRY.Text = he.CSALRY.Value.ToString();
                txt_JSALARY.Text = he.JSALARY.Value.ToString();
                txt_LSALRY.Text = he.LSALRY.Value.ToString();
                txt_OSALRY.Text = he.OSALRY.Value.ToString();
                txt_PERTAX.Text = he.PERTAX.Value.ToString();
                txt_PSALRY.Text = he.PSALRY.Value.ToString();
              //  txt_PERSAFE.Text = he.PERSAFE.Value.ToString();
                //Drp_Year.SelectedValue = he.YEAR.Value.ToString();
                //Drp_Mouth.SelectedValue = he.MONTH.Value.ToString();
            }
            else
            {
                txt_ABSENC.Text = "0";
                txt_ASALRY.Text = "0";
                txt_CSALRY.Text = "0";
                txt_JSALARY.Text = "0";
                txt_LSALRY.Text = "0";
                txt_OSALRY.Text = "0";
                txt_PERTAX.Text = "0";
                txt_PSALRY.Text = "0";
               // txt_PERSAFE.Text = "0";
            }

            //HR_Insurance _inSurance = TbsDbSession.Default.From<HR_Insurance>().Where(HR_Insurance._.EmpNo == Request["empid"] && HR_Insurance._.PayYear == int.Parse(Drp_Year.SelectedValue) && HR_Insurance._.PayMonth == int.Parse(Drp_Mouth.SelectedValue)).ToFirst();
            //if(_inSurance!=null)
            //    txt_PERSAFE.Text=_i
        }

        protected void BindGrid()
        {
            List<HR_SALRY> esList = TbsDbSession.Default.From<HR_SALRY>().Where(HR_SALRY._.EMPLID == Request["empid"]).ToList();
            if (esList.Count == 0)
            {
                HR_SALRY hr = new HR_SALRY();
                hr.ID = 0;
                hr.BSALRY = 0;
                hr.EMPLID = Request["empid"];
                hr.SSALRY = 0;
                hr.YSALRY = 0;
                esList.Add(hr);
            }
            Grid1.DataSource = esList;
            Grid1.DataBind();
        }

        protected void Grid1_AfterEdit(object sender, GridAfterEditEventArgs e)
        {
            Dictionary<int, Dictionary<string, string>> modifiedDict = Grid1.GetModifiedDict();
            int rowID = Convert.ToInt32(Grid1.DataKeys[e.RowIndex][0]);
            UpdateDataRow(modifiedDict[e.RowIndex], rowID);

            BindGrid();
        }

        protected void UpdateDataRow(Dictionary<string, string> rowDict, int rowid)
        {
            HR_SALRY hs = new HR_SALRY();
            if (rowid == 0)
            {
                hs.DeAttach();
            }
            else
            {
                hs.ID = rowid;
                hs.Attach();
            }
            hs.EMPLID = HttpContext.Current.Request["empid"];
            // 姓名
            if (rowDict.ContainsKey("BSALRY"))
            {
                hs.BSALRY = decimal.Parse(rowDict["BSALRY"]);
            }
            if (rowDict.ContainsKey("SSALRY"))
            {
                hs.SSALRY = decimal.Parse(rowDict["SSALRY"]);
            }
            if (rowDict.ContainsKey("YSALRY"))
            {
                hs.YSALRY = decimal.Parse(rowDict["YSALRY"]);
            }
            if (rowid == 0)
            {
                TbsDbSession.Default.Insert<HR_SALRY>(hs);
            }
            else
            {
                TbsDbSession.Default.Update<HR_SALRY>(hs);
            }
            int indexid = 0;
            HR_MSALY hmsaly = TbsDbSession.Default.From<HR_MSALY>().Where(HR_MSALY._.EMPLID == Request["empid"] && HR_MSALY._.YEAR == int.Parse(Drp_Year.SelectedValue) && HR_MSALY._.MONTH == int.Parse(Drp_Mouth.SelectedValue)).ToFirst();
            HR_MSALY hrm = new HR_MSALY();
            if (hmsaly == null)
            {
                hrm.DeAttach();
                hrm.EMPLID = Request["empid"];
                hrm.BSALRY = hs.BSALRY;
                hrm.SSALRY = hs.SSALRY;
                hrm.YSALRY = hs.YSALRY;
                hrm.YEAR = int.Parse(Drp_Year.SelectedValue);
                hrm.MONTH = int.Parse(Drp_Mouth.SelectedValue);
                indexid = TbsDbSession.Default.Insert<HR_MSALY>(hrm);
            }
            else
            {
                indexid = hmsaly.ID;
                hrm.ID = hmsaly.ID;
                hrm.Attach();
                hrm.BSALRY = hs.BSALRY;
                hrm.SSALRY = hs.SSALRY;
                hrm.YSALRY = hs.YSALRY;
                TbsDbSession.Default.Update<HR_MSALY>(hrm);
            }
            HR_MSALY tt = TbsDbSession.Default.From<HR_MSALY>().Where(HR_MSALY._.ID == indexid).ToFirst();
            HR_MSALY h_ms = new HR_MSALY();
            h_ms.ID = indexid;
            h_ms.Attach();
            h_ms.TOSALY = GetTotal(tt);
            TbsDbSession.Default.Update<HR_MSALY>(h_ms);
        }

        protected decimal GetTotal(HR_MSALY hm)
        {
            decimal dc = 0;
            decimal ASALRY = 0;
            if (hm.ASALRY.HasValue)
                ASALRY = hm.ASALRY.Value;

            decimal BSALRY = 0;
            if (hm.BSALRY.HasValue)
                BSALRY = hm.BSALRY.Value;

            decimal CSALRY = 0;
            if (hm.CSALRY.HasValue)
                CSALRY = hm.CSALRY.Value;

            decimal JSALARY = 0;
            if (hm.JSALARY.HasValue)
                JSALARY = hm.JSALARY.Value;

            decimal LSALRY = 0;
            if (hm.LSALRY.HasValue)
                LSALRY = hm.LSALRY.Value;


            decimal OSALRY = 0;
            if (hm.OSALRY.HasValue)
                OSALRY = hm.OSALRY.Value;


            decimal PSALRY = 0;
            if (hm.PSALRY.HasValue)
                PSALRY = hm.PSALRY.Value;



            decimal SSALRY = 0;
            if (hm.SSALRY.HasValue)
                SSALRY = hm.SSALRY.Value;


            decimal YSALRY = 0;
            if (hm.YSALRY.HasValue)
                YSALRY = hm.YSALRY.Value;


            decimal ABSENC = 0;
            if (hm.ABSENC.HasValue)
                ABSENC = hm.ABSENC.Value;


            decimal PERTAX = 0;
            if (hm.PERTAX.HasValue)
                PERTAX = hm.PERTAX.Value;

            decimal PERSAFE = 0;
            if (hm.PERSAFE.HasValue)
                PERSAFE = hm.PERSAFE.Value;


            return dc = ASALRY + BSALRY + CSALRY + JSALARY + LSALRY + OSALRY + PSALRY + SSALRY + YSALRY - ABSENC - PERTAX - PERSAFE;
            //HR_MSALY h_ms = new HR_MSALY();
            //h_ms.ID = hm.ID;
            //h_ms.Attach();
            //h_ms.TOSALY = dc;
            //TbsDbSession.Default.Update<HR_MSALY>(h_ms);
        }

        protected void btnP_Click(object sender, EventArgs e)
        {
            Drp_Mouth.SelectedValue = (int.Parse(Drp_Mouth.SelectedValue) - 1).ToString();
            YMBind();
        }

        protected void btnN_Click(object sender, EventArgs e)
        {
            if (Drp_Mouth.SelectedValue == "12")
                Drp_Year.SelectedValue = (int.Parse(Drp_Year.SelectedValue) + 1).ToString();
            Drp_Mouth.SelectedValue = (int.Parse(Drp_Mouth.SelectedValue) + 1).ToString();
            YMBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            HR_ESALY hr_Esaly = TbsDbSession.Default.From<HR_ESALY>().Where(HR_ESALY._.EMPLID == Request["empid"] && HR_ESALY._.YEAR == int.Parse(Drp_Year.SelectedValue) && HR_ESALY._.MONTH == int.Parse(Drp_Mouth.SelectedValue)).ToFirst();
            HR_ESALY hre = new HR_ESALY();
            if (hr_Esaly != null)
            {
                hre.ID = hr_Esaly.ID;
                hre.Attach();
                hre.ABSENC = decimal.Parse(txt_ABSENC.Text);
                hre.ASALRY = decimal.Parse(txt_ASALRY.Text);
                hre.CSALRY = decimal.Parse(txt_CSALRY.Text);
                hre.JSALARY = decimal.Parse(txt_JSALARY.Text);
                hre.LSALRY = decimal.Parse(txt_LSALRY.Text);
                hre.OSALRY = decimal.Parse(txt_OSALRY.Text);
                hre.PERTAX = decimal.Parse(txt_PERTAX.Text);
                hre.PSALRY = decimal.Parse(txt_PSALRY.Text);
               // hre.PERSAFE = decimal.Parse(txt_PERSAFE.Text);
                TbsDbSession.Default.Update<HR_ESALY>(hre);
            }
            else
            {
                hre.DeAttach();
                hre.EMPLID = Request["empid"];
                hre.ABSENC = decimal.Parse(txt_ABSENC.Text);
                hre.ASALRY = decimal.Parse(txt_ASALRY.Text);
                hre.CSALRY = decimal.Parse(txt_CSALRY.Text);
                hre.JSALARY = decimal.Parse(txt_JSALARY.Text);
                hre.LSALRY = decimal.Parse(txt_LSALRY.Text);
                hre.OSALRY = decimal.Parse(txt_OSALRY.Text);
                hre.PERTAX = decimal.Parse(txt_PERTAX.Text);
                hre.PSALRY = decimal.Parse(txt_PSALRY.Text);
                hre.YEAR = int.Parse(Drp_Year.SelectedValue);
                hre.MONTH = int.Parse(Drp_Mouth.SelectedValue);
               // hre.PERSAFE = decimal.Parse(txt_PERSAFE.Text);
                TbsDbSession.Default.Insert<HR_ESALY>(hre);
            }
            int indexid = 0;
            HR_MSALY hmsaly = TbsDbSession.Default.From<HR_MSALY>().Where(HR_MSALY._.EMPLID == Request["empid"] && HR_MSALY._.YEAR == int.Parse(Drp_Year.SelectedValue) && HR_MSALY._.MONTH == int.Parse(Drp_Mouth.SelectedValue)).ToFirst();
            HR_MSALY hrm = new HR_MSALY();
            if (hmsaly == null)
            {
                HR_SALRY sa = TbsDbSession.Default.From<HR_SALRY>().Where(HR_SALRY._.EMPLID == Request["empid"]).ToFirst();
                hrm.DeAttach();
                if (sa != null)
                {
                    hrm.BSALRY = sa.BSALRY;
                    hrm.SSALRY = sa.SSALRY;
                    hrm.YSALRY = sa.YSALRY;
                }
                hrm.EMPLID = Request["empid"];
                hrm.ABSENC = hre.ABSENC;
                hrm.ASALRY = hre.ASALRY;
                hrm.CSALRY = hre.CSALRY;
                hrm.JSALARY = hre.JSALARY;
                hrm.LSALRY = hre.LSALRY;
                hrm.OSALRY = hre.OSALRY;
                hrm.PERTAX = hre.PERTAX;
                hrm.PSALRY = hre.PSALRY;
                hrm.YEAR = hre.YEAR;
                hrm.MONTH = hre.MONTH;
                hrm.PERSAFE = hre.PERSAFE;
                indexid = TbsDbSession.Default.Insert<HR_MSALY>(hrm);
            }
            else
            {
                indexid = hmsaly.ID;
                hrm.ID = hmsaly.ID;
                hrm.Attach();
                hrm.ABSENC = hre.ABSENC;
                hrm.ASALRY = hre.ASALRY;
                hrm.CSALRY = hre.CSALRY;
                hrm.JSALARY = hre.JSALARY;
                hrm.LSALRY = hre.LSALRY;
                hrm.OSALRY = hre.OSALRY;
                hrm.PERTAX = hre.PERTAX;
                hrm.PSALRY = hre.PSALRY;
                hrm.YEAR = hre.YEAR;
                hrm.MONTH = hre.MONTH;
                hrm.PERSAFE = hre.PERSAFE;
                TbsDbSession.Default.Update<HR_MSALY>(hrm);
            }
            HR_MSALY tt = TbsDbSession.Default.From<HR_MSALY>().Where(HR_MSALY._.ID == indexid).ToFirst();
            HR_MSALY h_ms = new HR_MSALY();
            h_ms.ID = indexid;
            h_ms.Attach();
            h_ms.TOSALY = GetTotal(tt);
            TbsDbSession.Default.Update<HR_MSALY>(h_ms);
            Alert.Show("更新成功！");
        }

        protected void Drp_Year_SelectedIndexChanged(object sender, EventArgs e)
        {
            YMBind();
        }
    }
}